/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufpe.cin.dmo4.middleware.client;

import br.ufpe.cin.dmo4.middleware.io.SocketChannelHandler;
import br.ufpe.cin.dmo4.middleware.remotingerrors.RemotingError;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

/**
 *
 * @author Danilo
 */
public class ClientRequestHandler {

    private String serverAddress;

    public ClientRequestHandler(String address) {
        this.serverAddress = address;
    }

    public String getServerAddress() {
        return serverAddress;
    }

    public byte[] send(byte[] message) throws RemotingError{
        try {            
            SocketChannel socketChannel = SocketChannel.open();
            socketChannel.connect(new InetSocketAddress(serverAddress, 8891));
            
            SocketChannelHandler handler = new SocketChannelHandler(socketChannel);

            handler.writeBytes(message);

            byte[] resp = handler.getBytes();
            
            return resp;
        } catch (IOException ex) {
            throw new RemotingError();
        }
    }

}
